package com.deng.maintenance.vote.view
{
	import com.deng.maintenance.vote.vo.VoteDictVo;
	import com.deng.maintenance.vote.vo.VoteVsOrganizationVo;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.events.FlexEvent;
	import mx.resources.ResourceManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;

	public class SelectCB extends CheckBox
	{
		private var pvo:VoteVsOrganizationVo;
		public function SelectCB()
		{	
			this.addEventListener(FlexEvent.CREATION_COMPLETE,showHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,upEvt);
			this.addEventListener(FlexEvent.SHOW,showHandler);//本来是2
			this.addEventListener(KeyboardEvent.KEY_DOWN,keyHandler);
			super();
		}
		private function keyHandler(event:KeyboardEvent):void
		{
			if(event.keyCode == 32)
			{
				event.stopImmediatePropagation();
				return;
			}
		}
		private function insertRst(evt:ResultEvent):void{
			if(evt.result as int == 1){
				Alert.show(ResourceManager.getInstance().getString("lang", "update_success"));
			}else{
				Alert.show(ResourceManager.getInstance().getString("lang", "update_failed"));
				this.selected = false;
			}
			this.data = pvo;
		}
		private function deleteRst(evt:ResultEvent):void{
			if(evt.result as int == 1){
				Alert.show(ResourceManager.getInstance().getString("lang", "update_success"));
			}
			pvo.frq_code = null;
			pvo.vote_id = null;
			this.data = pvo;
		}
		private function upEvt(event:MouseEvent):void{
			pvo = this.data as VoteVsOrganizationVo;
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "VoteVsOrganizationImpl";			
			ro.addEventListener(FaultEvent.FAULT,flt);
			
			if(this.selected){//之前选中的，后面要删除
				ro.addEventListener(ResultEvent.RESULT,deleteRst);
				ro.deleteData(pvo);
			}else{//之前没选中，后面要增加
				try{
					ro.addEventListener(ResultEvent.RESULT,insertRst);
					pvo.vote_id = ((parent.parent.parent.parent as VoteVsOrganizationCV).itemArr.selectedItem as VoteDictVo).id;
					pvo.frq_code = (parent.parent.parent.parent as VoteVsOrganizationCV).peroidArr.selectedItem.value;
					
					ro.insertData(pvo);
				}catch(err:Error){
					Alert.show(err.getStackTrace());
				}
				
			}			
		}
		private function flt(evt:FaultEvent):void{
			Alert.show(evt.fault.getStackTrace());
		}
		private function showHandler(event:FlexEvent):void{
			pvo = this.data as VoteVsOrganizationVo;
			trace("1......"+pvo.frq_code);
			if( pvo.frq_code != null){
				this.selected = true;
			}else{
				this.selected = false;
			}
		}
//		private function showHandler2(event:FlexEvent):void{
//			pvo = this.data as VoteVsOrganizationVo;
//			trace("2......"+pvo.frq_code);
//			if( pvo.frq_code != null){
//				this.selected = true;
//			}else{
//				this.selected = false;
//			}
//		}
	}
}